import hashlib
import json
from datetime import datetime

import pymysql
from base.python_base_02 import *
import os;
__author__="Kiana";
'''
python  基础练习
'''

#========模块调用
def fun_11():
    try:
        for i in list(range(1,10)):
            print(i);
        a=1/1;
    except BaseException as be:
        print(be);
    finally:
        print("必须执行的代码块");

#===========文件读写=========
def fun_12():
    f=open(file="E:\test.tet",mode="r",encoding="utf-8")
    f.writelines(["fsdgs","\n","sdfsgs","\n"])
    f.close();

def fun_13():
    try:
        f=open(file="E:\test.tet",mode="r",encoding="utf-8");
        results=f.readline();
        print(results);
    except BaseException as e:
        print(e);
    finally:
        if f:
            f.close();

def fun_14():
    with open(file="E:\test.tet",mode="r",encoding="utf-8") as f:
        print(f.readline());

def fun_15():
    with open(file="E:/text.txt",mode="a",encoding="utf-8") as f:
        f.writelines(["添加第一行","\n","添加第二行","\n","添加第三行"]);

#=========OS操作==========
def fun_16():
    print(os.name);
    dest_folder="E:/test/aa/bb";
    if not os.path.exists(dest_folder):
        #os.mkdir(dest_folder)
        os.makedirs(dest_folder);
    os.rename("E:/test/rad.txt","E:/test/blue.txt");
    os.remove();

#==============mysql操作========
def get_connection_cursor():
    connection=pymysql.connect(host="127.0.0.1",port=3306,database="mydb",
                               user="root",password="RTlove2003",charset="utf8");
    cursor=connection.cursor();
    return  connection,cursor;

#=======执行增删改数据库=======
#执行增删改操作
def execute_(sql,cursor):
    return cursor.execute(sql);

#查询操作
def query_(sql,cursor):
    cursor.execute(sql);
    return cursor.fetchall();


#提交事务
def commit_(connection):
    connection.commit();

# 执行查询操作
def query_(sql, cursor):
    cursor.execute(sql);
    return cursor.fetchall();

#回滚事务
def rollback_(connection):
    if connection:
        connection.rollback();

#关闭连接
def close_(connection,cursor):
    if connection:
        connection.close();
    if cursor:
        cursor.close();

def fun_17():
    # sql 语句
    insert_sql = insert_sql = 'insert into user (user_name, password, create_date) ' \
                'values("%s", "%s", "%s")' % ("kiana", "1207", datetime.now().strftime("%Y-%m-%d %H:%M:%S"));
    query_sql = "select * from user";

    connection, cursor = None, None;
    try:
        # 获取链接对象、游标对象
        connection, cursor = get_connection_cursor();
        # 执行新增操作
        num = execute_(insert_sql, cursor);
        print(num);
        # 提交事务
        commit_(connection);

        # 执行查询操作
        result = query_(query_sql, cursor);
        print(result);
    except BaseException as e:
        print(e);
        # 回滚事务
        rollback_(connection);
    finally:
        # 关闭连接和游标
        close_(connection, cursor);

# ======== json ========
def fun_18():
    # json 字符串转 dict
    j = '{"name":"hj", "age":19, "gender":"male"}';
    print(j);
    print(json.loads(j));
    # dict 转 json
    d = {"color":"red", "name":"table", "price":34.45, "count":33};
    print(d);
    print(json.dumps(d));
    # 对象转 json 字符串
    user = User(user_name="hahhh", password="111111", email="hj@163.com");
    print(json.dumps(user.__dict__));
    # json 字符串转对象
    j = '{"user_name": "sss", "password": "2222", "email": "aa@163.com"}';
    user.__dict__ = json.loads(j);
    print(user.__dict__);

# ======== datetime ========
def fun_19():
    print(datetime.now());
    print(datetime.now().timestamp());
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"));
    print(datetime.strptime("2023-05-10 16:18:11", "%Y-%m-%d %H:%M:%S"));

# ======== hashlib ========
def fun_20(password):
    password = password + "salthj";
    print(hashlib.md5(password.encode("utf-8")).hexdigest());

if __name__ == '__main__':
    fun_20("heiheihei");